package com.lightheart.sphr.xiaobinggan.timeselection.ui

import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.TextView
import com.bigkoo.pickerview.OptionsPickerView
import com.bigkoo.pickerview.OptionsPickerView.OnOptionsSelectListener
import com.bigkoo.pickerview.TimePickerView
import com.bigkoo.pickerview.TimePickerView.OnTimeSelectListener
import com.lightheart.sphr.xiaobinggan.R
import com.lightheart.sphr.xiaobinggan.utils.RxTimer
import kotlinx.android.synthetic.main.activity_time_selection.*
import java.text.SimpleDateFormat
import java.util.*

class TimeSelectionActivity : AppCompatActivity() {

    private var birthOption: TimePickerView? = null
    private var sexSelect: OptionsPickerView<*>? = null
    private val sex = ArrayList<String>()
    private val title = ArrayList<String>()
    private var titleOption: OptionsPickerView<*>? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_time_selection)
        initTimeOption()
        initSexSelect()
        setTitles()

        etBirth.setOnClickListener {
            if (birthOption != null) birthOption?.show()
        }

        tvSex.setOnClickListener {
            if (sexSelect != null) sexSelect?.show()
        }

        etTitle.setOnClickListener {
            if (titleOption != null) titleOption!!.show()
        }

        val rxTimer = RxTimer()
        rxTimer.timer(500) { l: Long ->
            this.save(l)
        }

    }

    private fun save(l: Long) {
        title.add("老师")
        title.add("帅哥")
        title.add("美女")
        title.add("亲戚")
        title.add("大爷")
        sex.add(getString(R.string.male))
        sex.add(getString(R.string.female))

    }

    /**
     * 生日时间选择
     */
    private fun initTimeOption() {
        val nowDate = Calendar.getInstance() //系统当前时间
        val startDate = Calendar.getInstance()
        startDate[1900, 1] = 1
        birthOption = TimePickerView.Builder(this, OnTimeSelectListener { date, v -> //选中事件回调
            etBirth.text = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(date)
//            info.setBirth(date.time)
        })
                .setType(booleanArrayOf(true, true, true, false, false, false))
                .setContentSize(18)
                .setDate(nowDate)
                .setRangDate(startDate, nowDate)
                .setLayoutRes(R.layout.pickerview_time) { v ->
                    val tvSubmit = v.findViewById<View>(R.id.tv_finish) as TextView
                    val tv_cancel = v.findViewById<View>(R.id.tv_cancel) as TextView
                    tvSubmit.setOnClickListener {
                        birthOption?.returnData()
                        birthOption?.dismiss()
                    }
                    tv_cancel.setOnClickListener {
                        birthOption?.dismiss()
                    }
                }
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .build()
    }


    /**
     * 选择性别
     */
    private fun initSexSelect() {
        sexSelect = OptionsPickerView.Builder(this, OnOptionsSelectListener { options1, options2, options3, v ->
            tvSex.text = sex[options1]
        }).build()
        sexSelect?.setNPicker(sex, null, null)
    }


    /***
     * 职务选择器
     */
    fun setTitles() {
        titleOption = OptionsPickerView.Builder(this, OnOptionsSelectListener { options1, options2, options3, v ->
            etTitle.text = title[options1]
        }).setCancelColor(Color.parseColor("#888888"))
                .setSubmitColor(Color.parseColor("#888888"))
                .setTitleBgColor(Color.parseColor("#FFFFFF"))
                .build()
        titleOption?.setNPicker(title, null, null)
    }

}